//
//  ZThemeVC.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/10.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZThemeVC.h"
#import "ZZDeviceManager.h"
#import "ZZThemeCell.h"
#import "ZZThemeCellModel.h"
#import "ZZThemeCellModelGroup.h"
#import "ThemeCellModelTool.h"
#import "ZZZThemeSetVc.h"
#import "ZZMusicLightTool.h"
#import "ThemeCellModelToolB.h"

#import "ZZLightViewGroupModel.h"
#import "ZZLightViewCellModel.h"

#define kThemeCellIndentifier @"ThemeCellIndentifier"

@interface ZThemeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *selectBtn;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, weak)ZZDeviceManager *devicemanager;


@property (nonatomic, strong) ZZThemeCellModelGroup*modelGroup;
@property (nonatomic, strong) ZZThemeCellModelGroup*modelGroupB;
// A
@property (weak, nonatomic) IBOutlet UIButton *ABtn;

//B
@property (weak, nonatomic) IBOutlet UIButton *BBtn;
@property (weak, nonatomic) IBOutlet UIView *backImage;

@property (nonatomic, assign)BOOL isSelectB;

//

@end

@implementation ZThemeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [UIImageView zImageViewWithImage:@"img_logo"];
    
    
    self.navigationItem.rightBarButtonItem =  self.navigationItem.rightBarButtonItem = [self zNavItemWithImage:@"icon_lanmu" shouldOriginal:YES andSelector:@selector(rightNavItemClick)];
    
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:NSLocalizedString(@"THEME_ADD", nil) forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftDidClick) forControlEvents:UIControlEventTouchDown];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    leftBtn.frame = CGRectMake(0, 0, 44, 44);
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZZThemeCell" bundle:nil] forCellReuseIdentifier:kThemeCellIndentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.backImage.layer.cornerRadius = 16.5;
    self.backImage.layer.borderWidth = 2;
    self.backImage.layer.borderColor = [UIColor zColorWithRed:30 green:9 blue:10].CGColor;
    
    self.ABtn.layer.borderWidth = 2;
    self.ABtn.layer.cornerRadius = 16.5;
    self.ABtn.layer.masksToBounds = YES;
    self.ABtn.layer.borderColor = [UIColor redColor].CGColor;
    
    self.BBtn.layer.borderWidth = 2;
    self.BBtn.layer.cornerRadius = 16.5;
    self.BBtn.layer.masksToBounds = YES;
    self.BBtn.layer.borderColor = [UIColor clearColor].CGColor;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.isSelectB) {
        
        return self.modelGroupB.models.count;
    }
    return self.modelGroup.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZZThemeCell *cell = [tableView dequeueReusableCellWithIdentifier:kThemeCellIndentifier];
    cell.model = self.modelGroup.models[indexPath.row];
    cell.setBtnDidClick = ^(ZZThemeCellModel *model) {
      
        ZZZThemeSetVc *setVc = [[ZZZThemeSetVc alloc] init];
        setVc.view.backgroundColor;
      
        setVc.updateData = ^(ZZThemeCellModel *model){
          
            [self.tableView reloadData];
            
                
                [[ThemeCellModelToolB new] save];
            
                
                [[ThemeCellModelTool new] save];
            
        };
        
        if (_isSelectB) {
            
             setVc.model = self.modelGroupB.models[indexPath.row];
        } else {
            
            setVc.model = self.modelGroup.models[indexPath.row];
        }
        
        [self.navigationController pushViewController:setVc animated:YES];
    };
    
    if (self.isSelectB) {
        
        cell.model = self.modelGroupB.models[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}

#pragma mark - Func
- (void)rightNavItemClick {
    
    if (self.devicemanager) {
        
        [self.devicemanager removeFromSuperview];
        self.devicemanager = nil;
        return;
    }
    
    ZZDeviceManager *manager = [ZZDeviceManager xibInstance];
    manager.vc = self;
    
    self.devicemanager = manager;
    manager.frame = CGRectMake(Z_SCREEN_WIDTH, 64, Z_SCREEN_WIDTH, Z_SCREEN_HEIGHT - 64);
    [self.tabBarController.view addSubview:manager];
    
    [UIView animateWithDuration:0.4 animations:^{
        
        manager.x = 0;
    }];
}

- (void)leftDidClick {
    
    ZZThemeCellModel *model1 = [ZZThemeCellModel new];
    model1.title = @"默认名称";
    model1.modelStyle = 0;
    ZZColorRGBA *model1_color1 = [ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1];
    ZZColorRGBA *model1_color2 = [ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1];
    model1.pinLv = 0.5;
    model1.lightValue = 0.5;
    [model1.colors addObject:model1_color1];
    [model1.colors addObject:model1_color2];
    
    ZZZThemeSetVc *setVc = [[ZZZThemeSetVc alloc] init];
    setVc.view.backgroundColor;
    setVc.updateData = ^(ZZThemeCellModel *model){
        
        if (_isSelectB) { //选择A or B
            
            [self.modelGroupB.models insertObject:model atIndex:0];
        }else {
              [self.modelGroup.models insertObject:model atIndex:0];
        }
      
        [self.tableView reloadData];
        [[ThemeCellModelTool new] save];
        [[ThemeCellModelToolB new] save];
    };
    setVc.model = model1;
    
    [self.navigationController pushViewController:setVc animated:YES];
    
    
}

#pragma mark - action
- (IBAction)selectBtnAOrB:(UIButton *)sender {
    
    if (sender.tag == 1000) { // A
        
        self.isSelectB = NO;
        self.ABtn.layer.borderColor = [UIColor redColor].CGColor;
        self.BBtn.layer.borderColor = [UIColor clearColor].CGColor;
        
    } else {
        
        self.isSelectB = YES;
        self.ABtn.layer.borderColor = [UIColor clearColor].CGColor;
        self.BBtn.layer.borderColor = [UIColor redColor].CGColor;
    }
    [self.tableView reloadData];
}




#pragma mark - set & get
- (ZZThemeCellModelGroup *)modelGroup {
    
    if (!_modelGroup) {
        
        _modelGroup = [ThemeCellModelTool new].group;
    }
    return _modelGroup;
}

- (ZZThemeCellModelGroup *)modelGroupB {
    
    if (!_modelGroupB) {
        
        _modelGroupB = [ThemeCellModelToolB new].group;
    }
    return _modelGroupB;
}


//- (ZZLightViewCellModel *)duoSeModel {
//
//    if (!_duoSeModel) {
//
//       ZZLightViewGroupModel *groupModel = [[ZZMusicLightTool new] lightGroup];
//        ZZLightViewCellModel *model = nil;
//
//        for (ZZLightViewCellModel *cellModel in groupModel.models) {
//
//            if (cellModel.isSelect) {
//
//                model = cellModel;
//                break;
//            }
//        }
//
//        _duoSeModel = model;
//        if (_duoSeModel == nil) {
//
//            ZZLightViewCellModel *model2 = [ZZLightViewCellModel new];
//            model2.title = @"罗环";
//            model2.isSelect = YES;
//            model2.model = LightModelFugai;
//            model2.colors = [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]];
//            model2.imageSize = CGSizeMake(130, 130);
//            [model2 reloadImage];
//
//            _duoSeModel = model2;
//        }
//    }
//    return _duoSeModel;
//}
//
//- (ZZLightViewCellModel *)duoSeModelB {
//
//    if (!_duoSeModel) {
//
//        ZZLightViewGroupModel *groupModel = [[ZZMusicLightTool new] lightGroup];
//        ZZLightViewCellModel *model = nil;
//
//        for (ZZLightViewCellModel *cellModel in groupModel.models) {
//
//            if (cellModel.isSelect) {
//
//                model = cellModel;
//                break;
//            }
//        }
//
//        _duoSeModel = model;
//        if (_duoSeModel == nil) {
//
//            ZZLightViewCellModel *model2 = [ZZLightViewCellModel new];
//            model2.title = @"罗环";
//            model2.isSelect = YES;
//            model2.model = LightModelFugai;
//            model2.colors = [NSMutableArray arrayWithArray:@[[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:0 G:255.0 B:0 A:1],[ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1]]];
//            model2.imageSize = CGSizeMake(130, 130);
//            [model2 reloadImage];
//
//            _duoSeModel = model2;
//        }
//    }
//    return _duoSeModel;
//}


@end

